package com.springboot.springboot2.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName:
 * @author: CKC
 * @DateTime: 2021/1/28 22:50
 */
@Controller
public class RequestController {
    @GetMapping("/goto")
    public String goToPage(HttpServletRequest request){
        request.setAttribute("msg", "完成了·····");
        request.setAttribute("code", 200);
        return "forward:success";
    }
    
    @GetMapping("/params")
    public String testParams(Map<String, Object> map,
                             Model model,
                             HttpServletRequest request,
                             HttpServletResponse response) {
        
        map.put("hello", "world6666");
        model.addAttribute("world", "hello6666");
        request.setAttribute("message", "HelloWorld");
        response.addCookie(new Cookie("c1", "v1"));
        
        return "forward:success";
    }
    
    @ResponseBody
    @GetMapping("/success")
    public Map success(@RequestAttribute(value = "msg", required = false) String msg,
                       @RequestAttribute(value = "code", required = false) Integer code,
                       HttpServletRequest request) {
        
        Object msg1 = request.getAttribute("msg");
        
        Object hello = request.getAttribute("hello");
        Object world = request.getAttribute("world");
        Object message = request.getAttribute("message");
        
        
        Map<String, Object> map = new HashMap<>();
        map.put("reqMethod_msg", msg1);
        map.put("annotation_msg", msg);
        
        map.put("hello", hello);
        map.put("world", world);
        map.put("message", message);
        
        return map;
    }
    /**
     * servlet API ：
     *     WebRequest、ServletRequest、MultipartRequest、HttpSession、javax.servlet.http.PushBuilder、
     *     Principal、InputStream、Reader、HttpMethod、Locale、TimeZone、Zoneld
     *
     * 复杂参数：
     *     Map、Model (map、model里面的数据会被放在request的请求域request.setAttribute) ,
     *     RedirectAttributes（重定向时携带数据）、ServletResponse、SessionStatus、UriComponentsBuilder、
     *     Errors/BindingResult、
     *     ServleturiComponentsBuilder
     *
     * SpringMVC到底支持哪些返回值？
     *     ModelAndView
     *     Model
     *     View
     *     ResponseEntity
     *     ResponseBodyEmitter
     *     StreamingResponseBody
     *     HttpEntity
     *     HttpHeaders
     *     Callable
     *     DeferredResult
     *     ListenableResult
     *     ListenableFuture
     *     CompletionStage
     *     WebAsyncTask
     *
     *     有@ModelAttribute
     *     @ResponseBody注解 ----> RequestResponseBodyMethodProcessor
     */
    
    
}
